﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Collections.Specialized;

namespace LogicProtocolAnalyzer
{
    public class Settings
    {
        /// <summary>Selected port name</summary>
        internal string PortName { get; set; }

        /// <summary>Alphanumerical sorted list of all comports on this system</summary>
        internal string[] PortNames
        {
            get
            {
                string[] portNames = SerialPort.GetPortNames();
                Array.Sort(portNames, new AlphanumComparatorFast());
                return portNames;
            }
        }

        /// <summary>The trigger mode (auto compare, auto edge and manual)</summary>
        internal TriggerMode TriggerMode { get; set; }

        // Auto compare
        private byte _autoCompareLevels;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="channelIndex"></param>
        /// <returns></returns>
        internal Level GetAutoCompareTriggerLevel(int channelIndex)
        {
            int filter = 1 << channelIndex;
            return ((_autoCompareLevels & filter) == 0) ? Level.Low : Level.High;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="channelIndex"></param>
        /// <param name="level"></param>
        internal void SetAutoCompareTriggerLevel(int channelIndex, Level level)
        {
            byte filter = (byte)(1 << channelIndex);
            if (level == Level.High)
            {
                _autoCompareLevels |= filter;   // Set bit
            }
            else
            {
                _autoCompareLevels &= (byte)~filter; // Clear bit
            }
        }

        /// <summary>Channel (0..7) used to auto trigger on edge</summary>
        internal int AutoEdgeChannelIndex { get; set; }

        /// <summary>Auto edge trigger direction (rising or falling)</summary>
        internal TriggerEdge AutoEdgeDirection { get; set; }

        /// <summary>Sample frequency</summary>
        internal SampleFrequency SampleFrequency { get; set; }

        /// <summary>Number of sample pages</summary>
        internal int SampleSize { get; set; }

        /// <summary>Load the settings from the configuration file</summary>
        internal void Load()
        {
            this.PortName = Properties.Settings.Default.PortName;
            this.SampleFrequency = (SampleFrequency)Properties.Settings.Default.SampleFrequencyIndex;
            this.SampleSize = Properties.Settings.Default.NumberOfSamplePages;
            this.TriggerMode = (TriggerMode)Properties.Settings.Default.TriggerMode;
            this.AutoEdgeChannelIndex = Properties.Settings.Default.AutoEdgeChannelIndex;
            this.AutoEdgeDirection = (TriggerEdge)Properties.Settings.Default.AutoEdgeDirection;
            _autoCompareLevels = Properties.Settings.Default.AutoCompareChannelLevels;
        }

        /// <summary>Save the settings to the configuration file</summary>
        internal void Save()
        {
            Properties.Settings.Default.PortName = this.PortName;
            Properties.Settings.Default.SampleFrequencyIndex = (int)this.SampleFrequency;
            Properties.Settings.Default.NumberOfSamplePages = this.SampleSize;
            Properties.Settings.Default.TriggerMode = (int)this.TriggerMode;
            Properties.Settings.Default.AutoEdgeChannelIndex = (int)this.AutoEdgeChannelIndex;
            Properties.Settings.Default.AutoEdgeDirection = (int)this.AutoEdgeDirection;
            Properties.Settings.Default.AutoCompareChannelLevels = _autoCompareLevels;

            Properties.Settings.Default.Save();
        }
    }
}
